//
//  MoviesCollectionViewCell.swift
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit

class MoviesCollectionViewCell: UICollectionViewCell {
    
    var cellModel: MoviesCellModel? { didSet {
        guard let model = cellModel else { return }
        numberLabel.text = model.number
//        guard let url = URL(string: model.materials) else { return }
//        imageView.kf.setImage(with: url)
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
//    override func prepareForReuse() {
//        super.prepareForReuse()
//        print("-->toj: func: prepareForReuse MoviesCollectionViewCell")
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /* numberLabel */
    private lazy var numberLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.textColor = .black
        return view
    }()
    
    /* image */
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        return view
    }()
}

extension MoviesCollectionViewCell: UICollectionViewCellDataSource {
    
    var dataModel: UICollectionViewCellModelProtocol? {
        get {
            return cellModel
        }
        set {
            cellModel = newValue as? MoviesCellModel
        }
    }
}

// MARK: - 初始化
extension MoviesCollectionViewCell {
    
    private func setupMakeAddSubviews() {
        contentView.addSubview(imageView)
        contentView.addSubview(numberLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        numberLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.centerY.equalTo(contentView)
        }
        imageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
    
    private func setupMakeInitialize() {
        contentView.backgroundColor = .random
    }
}
